package com.yang.guliware.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.guliware.entity.WmsWareSku;
import com.yang.guliware.service.impl.WmsWareSkuServiceImpl;
import com.yang.guliware.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品库存 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-08-02
 */
@RestController
@RequestMapping("/guliware/wms-ware-sku")
public class WmsWareSkuController {

    @Autowired
    private WmsWareSkuServiceImpl wmsWareSkuService;

    @PostMapping("getWareSkuPage/{current}/{limit}")
    public R getWareSkuPage(@PathVariable Long current, @PathVariable Long limit,
                            @RequestBody(required = false) WmsWareSku wmsWareSku){
        QueryWrapper<WmsWareSku> queryWrapper = new QueryWrapper<>();
        Page<WmsWareSku> page = new Page<>(current,limit);
        String skuName = wmsWareSku.getSkuName();
        Long wareId = wmsWareSku.getWareId();
        if (!StringUtils.isEmpty(wareId)){
            queryWrapper.eq("ware_id",wareId);
        }
        if (!StringUtils.isEmpty(skuName)){
            queryWrapper.eq("sku_name",skuName);
        }
        wmsWareSkuService.page(page,queryWrapper);
        List<WmsWareSku> records = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("records",records).data("total",total);
    }

    @PostMapping("addWareSku")
    public R addWareSku(@RequestBody WmsWareSku wmsWareSku){
        wmsWareSkuService.save(wmsWareSku);
        return R.ok();
    }

    @PostMapping("updateWareSku")
    public R updateWareSku(@RequestBody WmsWareSku wmsWareSku){
        wmsWareSkuService.updateById(wmsWareSku);
        return R.ok();
    }

    @GetMapping("getById/{id}")
    public R getById(Long id){
        WmsWareSku wareSku = wmsWareSkuService.getById(id);
        return R.ok().data("wareSku",wareSku);
    }

    @DeleteMapping("dateleWareSku/{id}")
    public R dateleWareSku(Long id){
        wmsWareSkuService.removeById(id);
        return R.ok();
    }


}

